import React from 'react'
import {
  Form,
  Input,
  Button,
  Toast
} from 'antd-mobile'
import axios from '../../request/index'
import { useSearchParams ,useNavigate} from 'react-router-dom'

const Login = () => {
  const [searchParams] = useSearchParams()
  const navigate = useNavigate()
  const onFinish=async(values)=>{
    let res = await axios.post('/api/login',values)
    if(res.data.code==200){
      localStorage.setItem('token',res.data.data.token)
      Toast.show({
        content:'登录成功'
      })
      if(searchParams.get('from')){
        let from = searchParams.get('from')
        console.log(searchParams.get('from'))
        navigate(`/${from}`)
      }else{
        navigate('/')
      }
      
    }
  }
  
  return (
    <div>
         <Form
        layout='horizontal'
        onFinish={onFinish}
        footer={
         <>
          <Button block type='submit' color='primary' size='large'>
            登录
          </Button>
          <Button block size='large'>
          注册
        </Button>
        </>
        }
      >
        <Form.Header>欢迎登录</Form.Header>
        <Form.Item
          name='username'
          label='用户'
          rules={[{ required: true, message: '用户不能为空' }]}
        >
          <Input placeholder='用户名/邮箱/手机号' />
        </Form.Item>
        <Form.Item
          name='password'
          label='密码'
          rules={[{ required: true, message: '密码不能为空' }]}
        >
          <Input placeholder='登录密码' />
        </Form.Item>
        </Form>
    </div>
  )
}

export default Login
